package lc.com.lcpicturebackend.service.impl;

import lc.com.lcpicturebackend.service.SseService;
import lc.com.lcpicturebackend.utils.MarkdownUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;

// src/main/java/com/xxx/server/service/impl/SseServiceImpl.java

@Service
public class SseServiceImpl implements SseService {

    @Override
    public void handleSseMessage(String chunk, StringBuilder markdownBuffer, SseEmitter emitter) throws IOException {
        // 处理标题（###开头）
        if (chunk.trim().startsWith("###")) {
            // 先发送之前的内容
            if (markdownBuffer.length() > 0) {
                emitter.send(markdownBuffer.toString() + "\n\n");
                markdownBuffer.setLength(0);
            }
            // 发送标题，确保标题前后有足够的换行
            emitter.send("\n" + chunk + "\n");
            return;
        }

        // 处理图片链接
        if (chunk.contains("![") && chunk.contains("](")) {
            // 先发送之前的内容
            if (markdownBuffer.length() > 0) {
                emitter.send(markdownBuffer.toString() + "\n");
                markdownBuffer.setLength(0);
            }
            // 发送图片链接，确保前后有换行
            emitter.send("\n" + chunk + "\n\n");
            return;
        }

        // 处理选项（数字+)开头）
        if (chunk.trim().matches("^\\d+\\).*")) {
            // 先发送之前的内容
            if (markdownBuffer.length() > 0) {
                emitter.send(markdownBuffer.toString() + "\n");
                markdownBuffer.setLength(0);
            }
            // 发送选项，确保有换行
            emitter.send(chunk + "\n");
            return;
        }

        // 处理普通段落结束
        if (isParagraphEnd(chunk)) {
            markdownBuffer.append(chunk);
            emitter.send(markdownBuffer.toString() + "\n\n");
            markdownBuffer.setLength(0);
            return;
        }

        // 处理换行符
        if (chunk.contains("\n")) {
            String[] lines = (markdownBuffer.toString() + chunk).split("\n");
            markdownBuffer.setLength(0);

            for (int i = 0; i < lines.length; i++) {
                String line = lines[i].trim();
                if (!line.isEmpty()) {
                    emitter.send(line + "\n");
                }
            }
            return;
        }

        // 累积普通文本
        markdownBuffer.append(chunk);
    }

    private boolean isParagraphEnd(String chunk) {
        return chunk.endsWith("。") || chunk.endsWith("！") ||
                chunk.endsWith("？") || chunk.endsWith(".") ||
                chunk.endsWith("!") || chunk.endsWith("?") ||
                chunk.endsWith("：") || chunk.endsWith(":");
    }

    @Override
    public void handleRemainingContent(StringBuilder markdownBuffer, SseEmitter emitter) throws IOException {
        if (markdownBuffer.length() > 0) {
            String content = markdownBuffer.toString().trim();
            if (!content.isEmpty()) {
                emitter.send(content + "\n");
            }
        }
    }
}